import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * 实现信息管理系统 增、删、改、查和遍历的功能
 */

public class Test {

    public static void main(String[] args) {

        //判断信息管理系统是否继续运行
        boolean flag = true;
        //1.初始化信息管理系统类
        List<StudentInformationManagementSystem> student = new LinkedList<StudentInformationManagementSystem>();
        StudentInformationManagementSystem studentInf = null;
        Scanner sc = new Scanner(System.in);
        //4.ObjectOutputStream 和ObjectInputStream类 并将数据写入文件中
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        //5.在启动信息管理系统时，将文件中的数据传入集合中
        //5.1 关联文件
        try {
            ois = new ObjectInputStream(new FileInputStream("data.txt"));
            //5.2 从输入读取一个对象并打印
            Object obj = ois.readObject();
            student = (List<StudentInformationManagementSystem>)obj;

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        while (flag) {
            //2.让用户选择需要进行什么操作
            //1 增加 2 删除 3 修改 4 查看 5 遍历学生信息
            System.out.println("请选择需要执行的操作：");
            System.out.println("1.增加学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.查看学生信息");
            System.out.println("5.遍历学生信息");
            System.out.println("0.关闭信息管理系统");
            int index = sc.nextInt();
            switch (index){
                //2.1增加功能
                case 1:
                    int num = 0;
                    studentInf = new StudentInformationManagementSystem();
                    //3 抛出的学号或者年龄异常
                    System.out.println("请输入学生的学号：");
                    try {
                        studentInf.setId(sc.nextInt());
                        System.out.println("请输入学生的年龄：");
                        try {
                            studentInf.setAge(sc.nextInt());
                            System.out.println("请输入学生的姓名：");
                            studentInf.setName(sc.next());
                            int len = student.size();
                            student.add(len,studentInf);
                        } catch (AgeException e) {
                            e.printStackTrace();
                        }
                    } catch (IdException e) {
                        e.printStackTrace();
                    }

                    break;

                //2.2 删除功能
                case 2:
                    System.out.println("请输入要删除的学生的学号：");
                    int studentId = sc.nextInt();
                    int number = 0;
                    for(StudentInformationManagementSystem l:student){
                        if (l.getId() == studentId){
                            break;
                        }
                        number++;
                    }
                    student.remove(number);
                    break;

                //2.3 修改功能
                case 3:
                    System.out.println("请输入要修改的学生的学号：");
                    studentId = sc.nextInt();
                    number = 0;
                    for(StudentInformationManagementSystem l:student){
                        if (l.getId() == studentId){
                            break;
                        }
                        number++;
                    }
                    studentInf = new StudentInformationManagementSystem();
                    //3 抛出的学号或者年龄异常
                    System.out.println("请输入学生的学号：");
                    try {
                        studentInf.setId(sc.nextInt());
                        try {
                            System.out.println("请输入学生的年龄：");
                            studentInf.setAge(sc.nextInt());
                            System.out.println("请输入学生的姓名：");
                            studentInf.setName(sc.next());

                            student.set(number,studentInf);
                        } catch (AgeException e) {
                            e.printStackTrace();
                        }
                    } catch (IdException e) {
                        e.printStackTrace();
                    }
                    break;

                //2.4 查看功能
                case 4:
                    System.out.println("请输入要查看的学生的学号：");
                    studentId = sc.nextInt();
                    number = 0;
                    for(StudentInformationManagementSystem l:student){
                        if (l.getId() == studentId){
                            break;
                        }
                        number++;
                    }
                    studentInf = student.get(number);
                    System.out.println("学号：" + studentInf.getId() + "年龄：" + studentInf.getAge() + "姓名：" + studentInf.getName());
                    break;

                //2.5 遍历学生信息
                case 5:
                    for(StudentInformationManagementSystem l:student){
                        System.out.println("学号：" + l.getId() + "年龄：" + l.getAge() + "姓名：" + l.getName());
                    }
                    break;

                //2.6 退出信息管理系统
                case 0:
                    try {
                        flag = false;
                        //与文件相关联
                        oos = new ObjectOutputStream(new FileOutputStream("data.txt"));
                        //将整个LinkedList类型写入输出流
                        oos.writeObject(student);
                        System.out.println("数据保存成功！！！");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        //关闭流
                        if (oos != null){
                            try {
                                oos.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    }
}
